package com.coderbean.demo.leaveapply.listener;

import com.coderbean.demo.util.SpringContextUtils;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.runtime.ProcessInstance;

import com.coderbean.demo.leaveapply.entity.TLeaveApplyModel;
import com.coderbean.demo.leaveapply.service.ITLeaveApplyService;

public class EndListener implements ExecutionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3248048906845500860L;

	@Override
	public void notify(DelegateExecution execution) {
		String processId = execution.getProcessInstanceId();
		RuntimeService runtimeService = SpringContextUtils.getBean(RuntimeService.class);
		ProcessInstance processInstanc = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
		String leaveId = processInstanc.getBusinessKey();
		if (!org.springframework.util.StringUtils.isEmpty(leaveId)) {
			ITLeaveApplyService leaveApplyService = SpringContextUtils.getBean(ITLeaveApplyService.class);
			TLeaveApplyModel leaveApplyModel = leaveApplyService.getById(leaveId);
			leaveApplyModel.setApproveStatus(2);
			leaveApplyService.updateById(leaveApplyModel);
		}
	}

}
